package com.jmpt.fangbohui.ui.personalcenter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.UserRegisterBean;
import com.jmpt.fangbohui.ui.base.BaseActivity;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.utils.ToastHelper;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
/**
 * 
 *
 * @package com.jmpt.fangbohui.ui.personalcenter
 * @author cangJie
 * @description 用户注册页面
 *
 */
public class UserRegisterActivity extends BaseHeadActivity
{

	protected static final String TAG = "UserRegisterActivity";
	@ViewInject(R.id.et_activity_user_register_name)
	private EditText et_activity_user_register_name;
	@ViewInject(R.id.et_activity_user_register_phone)
	private EditText et_activity_user_register_phone;
	@ViewInject(R.id.et_activity_user_register_password)
	private EditText et_activity_user_register_password;
	@ViewInject(R.id.et_activity_user_register_password_config)
	private EditText et_activity_user_register_password_config;
	@ViewInject(R.id.tv_base_head_title)
	private TextView tv_base_head_title;
	@ViewInject(R.id.btn_base_head_back)
	private Button btn_base_head_back;
	@ViewInject(R.id.btn_base_head_right_button)
	private Button btn_base_head_right_button;
	private final int SUCCESS = 100;
	private final int FAILED = 102;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			hideLoading();
			switch(msg.what){
			case SUCCESS:
				String result = (String) msg.obj;
				Gson gson = new Gson();
				UserRegisterBean registerBean = gson.fromJson(result, UserRegisterBean.class);
				Log.d(TAG, "result:"+registerBean);
				String registerResult = registerBean.getMsg();
				if(registerResult.equals("注册成功")){
					finish();
				}
				ToastHelper.ShowToast(registerResult, UserRegisterActivity.this);
				break;
			case FAILED:
				ToastHelper.ShowToast("网络异常，请稍后再试", UserRegisterActivity.this);
				break;
			}
		};
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView()
	{
		setContentView(R.layout.activity_user_register);
		ViewUtils.inject(this);
		showTitle("用户注册");
		showBackButton(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	public void register(View view){
		String password = et_activity_user_register_password.getText().toString().trim();
		String passwordConfig = et_activity_user_register_password_config.getText().toString().trim();
		String name = et_activity_user_register_name.getText().toString().trim();
		String phone = et_activity_user_register_phone.getText().toString().trim();
		if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(password)||TextUtils.isEmpty(passwordConfig)||TextUtils.isEmpty(name)){
			ToastHelper.ShowToast("*项不能为空", this);
			return;
		}
		
		if(!password.equals(passwordConfig)){
			ToastHelper.ShowToast("两次输入密码不一致", this);
			return;
		}
		if(passwordConfig.length()<6){
			ToastHelper.ShowToast("密码长度需要大于或等于六位", this);
			return;
		}
		RequestParams params = new RequestParams ();
		params.addBodyParameter("username", name);
		params.addBodyParameter("phone", phone);
		params.addBodyParameter("password", password);
		showLoading();
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, Constants.URL.USERREGISTERURL, params, new RequestCallBack<String>()
		{

			@Override
			public void onFailure(HttpException arg0, String arg1)
			{
				Message obtain = Message.obtain();
				obtain.what = FAILED;
				handler.sendMessage(obtain);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0)
			{

				String result = arg0.result;
				int start = result.indexOf("{");
				int end = result.indexOf("}") + 2;
				result = result.substring(start, end);
				Message obtain = Message.obtain();
				obtain.obj = result;
				obtain.what = FAILED;
				handler.sendMessage(obtain);
				
			}
		});
	}

}
